<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="人员" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作类型" prop="operType">
        <el-select v-model="queryParams.operType" placeholder="请选择操作类型" clearable size="small">
          <el-option
            v-for="opertype in opertypeList"
            :key="opertype.id"
            :label="opertype.name"
            :value="opertype.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['account:moneyadd:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['account:moneyadd:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="moneyaddList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="人员" align="center" prop="nickName" />
      <el-table-column label="金额（元）" align="center" prop="amount" />
      <el-table-column label="操作类型" align="center" prop="operTypeName" />
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column label="操作人" align="center" prop="createBy" />
      <el-table-column label="操作时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改帐户增款对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="手机"   :inline="true">
          <el-input v-model="mobile" placeholder="请输入手机号" style="width:200px" maxlength="11"/>
          <el-button type="primary" size="small" @click="queryUserByMobile">查询</el-button>
          <span style="margin-left: 10px;color:firebrick">{{userName}}</span>
        </el-form-item>
        <el-form-item label="人员ID"  prop="userId" >
          <el-input v-model="form.userId" readonly/>
        </el-form-item>
        <el-form-item label="金额" prop="amount">
          <el-input v-model="form.amount" type="number" placeholder="请输入金额" />
        </el-form-item>
        <el-form-item label="操作类型" prop="operType">
          <el-select v-model="form.operType" placeholder="请选择操作类型" clearable >
            <el-option
              v-for="opertype in opertypeList"
              :key="opertype.id"
              :label="opertype.name"
              :value="opertype.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
    import { listMoneyadd, getMoneyadd, delMoneyadd, addMoneyadd, updateMoneyadd, exportMoneyadd } from "@/api/account/moneyadd";
    import { listUser } from "@/api/system/user";
    import { listOpertype } from "@/api/account/opertype";


    export default {
        name: "Moneyadd",
        data() {
            return {
                // 遮罩层
                loading: true,
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 帐户增款表格数据
                moneyaddList: [],
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    userId: null,
                    amount: null,
                    operType: null,
                    remarks: null,
                },
                // 表单参数
                form: {},
                // 表单校验
                rules: {
                    userId: [
                        { required: true, message: "人员不能为空，请输入手机号后点击搜索", trigger: "blur" }
                    ],
                    amount: [
                        { required: true, message: "金额不能为空", trigger: "blur" }
                    ],
                    operType: [
                        { required: true, message: "操作类型不能为空", trigger: "blur" }
                    ],
                },
                mobile:undefined,
                userName:undefined,
                // 账户操作类型表格数据
                opertypeList: [],
            };
        },
        created() {
            this.getList();
            this.getOpertypeList();

        },
        methods: {
            /** 查询账户操作类型列表 */
            getOpertypeList() {
                this.loading = true;
                listOpertype({balanceChange:-1,status:0}).then(response => {
                    this.opertypeList = response.rows;
                    console.log(this.opertypeList);
                    this.loading = false;
                });
            },
            /** 根据手机号查询用户 */
            queryUserByMobile() {
                this.userName = undefined;
                this.form.userId = undefined;
                this.loading = true;
                listUser({phonenumber:this.mobile}).then(response => {
                        this.loading = false;
                        if(response.rows[0]){
                            this.form.userId = response.rows[0].userId;
                            this.userName = "姓名：" + response.rows[0].nickName;
                            this.cardNo = response.rows[0]
                        }else{
                            this.userName = "用户不存在" ;
                        }

                    }
                );
            },
            /** 查询帐户增款列表 */
            getList() {
                this.loading = true;
                this.queryParams.balanceChange = -1;//增款
                listMoneyadd(this.queryParams).then(response => {
                    this.moneyaddList = response.rows;
                    this.total = response.total;
                    this.loading = false;
                });
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                    id: null,
                    userId: null,
                    amount: null,
                    operType: null,
                    remarks: null,
                    createBy: null,
                    createTime: null,
                    updateBy: null,
                    updateTime: null
                };
                this.resetForm("form");
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm("queryForm");
                this.handleQuery();
            },
            // 多选框选中数据
            handleSelectionChange(selection) {
                this.ids = selection.map(item => item.id)
                this.single = selection.length!==1
                this.multiple = !selection.length
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.reset();
                this.open = true;
                this.title = "添加帐户增款";
                this.mobile = undefined;
                this.userName = undefined;
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset();
                const id = row.id || this.ids
                getMoneyadd(id).then(response => {
                    this.form = response.data;
                    this.open = true;
                    this.title = "修改帐户增款";
                });
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        if (this.form.id != null) {
                            updateMoneyadd(this.form).then(response => {
                                this.msgSuccess("修改成功");
                                this.open = false;
                                this.getList();
                            });
                        } else {
                            addMoneyadd(this.form).then(response => {
                                this.msgSuccess("新增成功");
                                this.open = false;
                                this.getList();
                            });
                        }
                    }
                });
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                const ids = row.id || this.ids;
                this.$confirm('是否确认删除帐户增款编号为"' + ids + '"的数据项?', "警告", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(function() {
                    return delMoneyadd(ids);
                }).then(() => {
                    this.getList();
                    this.msgSuccess("删除成功");
                })
            },
            /** 导出按钮操作 */
            handleExport() {
                const queryParams = this.queryParams;
                this.$confirm('是否确认导出所有帐户增款数据项?', "警告", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(function() {
                    return exportMoneyadd(queryParams);
                }).then(response => {
                    this.download(response.msg);
                })
            }
        }
    };
</script>
